home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************/
- /* */
- /* TurboCAD for Windows */
- /* Copyright (c) 1993 - 2001 */
- /* International Microcomputer Software, Inc. */
- /* (IMSI) */
- /* All rights reserved. */
- /* */
- /******************************************************************/
-
- // ViewPict.cpp : Defines the entry point for the DLL application.
- //
-
- #include "stdafx.h"
- #include "ViewPict.h"
-
- BOOL APIENTRY DllMain( HANDLE hModule,
- DWORD ul_reason_for_call,
- LPVOID lpReserved
- )
- {
- switch (ul_reason_for_call)
- {
- case DLL_PROCESS_ATTACH:
- case DLL_THREAD_ATTACH:
- case DLL_THREAD_DETACH:
- case DLL_PROCESS_DETACH:
- break;
- }
- return TRUE;
- }
-
- // IN: DIB memory object
- // RETURNS: StdPicture object
- // SIDE EFFECTS: original memory object is destroyed
- // StdPicture that is returned owns a DDB created from the original DIB
- VIEWPICT_API IDispatch* __stdcall BitmapToPicture(HGLOBAL hDIB)
- {
- // Convert DIB into DDB for use in PICTDESC
- BITMAPINFOHEADER* lpbi = (BITMAPINFOHEADER*)GlobalLock(hDIB);
- WORD nNumColors = (WORD)lpbi->biClrUsed;
- if (nNumColors == 0 && lpbi->biBitCount < 24)
- nNumColors = 1 << lpbi->biBitCount; /* standard size table */
- WORD offBits = (WORD)lpbi->biSize + nNumColors * sizeof(RGBQUAD);
- LPSTR lpBits = (LPSTR)lpbi + offBits;
-
- HDC hDC = GetDC(NULL);
- HBITMAP hBitmap = CreateCompatibleBitmap(hDC, (WORD)lpbi->biWidth, (WORD)lpbi->biHeight);
- ::SetDIBits(hDC, hBitmap, 0, (WORD)lpbi->biHeight, lpBits, (BITMAPINFO*)lpbi, DIB_RGB_COLORS);
-
- // Done with original DIB
- GlobalUnlock(hDIB);
- GlobalFree(hDIB);
- hDIB = NULL;
-
- // Transfer ownership of DDB to StdPicture object
- PICTDESC pd;
- ZeroMemory(&pd, sizeof(pd));
- pd.cbSizeofstruct = sizeof(pd);
- pd.picType = PICTYPE_BITMAP;
- pd.bmp.hbitmap = hBitmap;
- pd.bmp.hpal = 0;
-
- IDispatch* pPicture = NULL;
- HRESULT hr = OleCreatePictureIndirect(&pd, IID_IDispatch, TRUE, (void**)&pPicture);
-
- // Done with DC
- ReleaseDC(NULL, hDC);
-
- if (FAILED(hr) || pPicture == NULL)
- {
- // Destroy failed bitmap
- DeleteObject(hBitmap);
- return 0;
- }
-
- // Success
- return pPicture;
- }
-
- // IN: Metafile handle
- // RETURNS: StdPicture object
- // SIDE EFFECTS: original Metafile object is destroyed on error,
- // otherwise, it is owned by the returned StdPicture object.
- VIEWPICT_API IDispatch* __stdcall MetafileToPicture(HMETAFILE hMF, long width, long height)
- {
- // Transfer ownership of Metafile to StdPicture object
- PICTDESC pd;
- ZeroMemory(&pd, sizeof(pd));
- pd.cbSizeofstruct = sizeof(pd);
- pd.picType = PICTYPE_METAFILE;
- pd.wmf.hmeta = (HMETAFILE)hMF;
- pd.wmf.xExt = width;
- pd.wmf.yExt = height;
-
- IDispatch* pPicture = NULL;
- HRESULT hr = OleCreatePictureIndirect(&pd, IID_IDispatch, TRUE, (void**)&pPicture);
- if (FAILED(hr) || pPicture == NULL)
- {
- // Destroy failed metafile
- DeleteMetaFile(hMF);
- return 0;
- }
-
- // Success
- return pPicture;
- }
-
-
-